
<%@ page language="java" contentType="text/html" 
    pageEncoding="UTF-8" %>
    

<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.stream.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.MalformedURLException" %>

<%@ page import="java.net.HttpURLConnection" %>

<% 

  String itemId = request.getParameter("item_id");
  URL url_item = new URL("http://api.powerhousemuseum.com/api/v1/item/" + itemId +"/xml/?api_key=90b0723f9926ce3");
  HttpURLConnection conn = (HttpURLConnection) url_item.openConnection();

  //InputStream file = conn.getInputStream();
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  
  Document doc_item = db.parse(conn.getInputStream());
  
  /* Get multimedia url */
  NodeList multimediaList = doc_item.getElementsByTagName("multimedia_uri");
  Node multimedia = multimediaList.item(0).getFirstChild();
  String multimediaUri = multimedia.getNodeValue();
  URL url_multimedia = new URL("http://api.powerhousemuseum.com"+multimediaUri);
  
  HttpURLConnection conn_multimedia = (HttpURLConnection) url_multimedia.openConnection();
  Document doc_multimedia = db.parse(conn_multimedia.getInputStream());
  
  /*
  NodeList thumbnails = doc_item.getElementsByTagName("thumbnail");
  String thumbnail_url = thumbnails.item(0).getFirstChild().getFirstChild().getNodeValue();
	*/
  //Using xslt 
  Transformer transformer = TransformerFactory .newInstance().newTransformer(new StreamSource(application.getResourceAsStream("item.xsl")));	
  transformer.transform(new StreamSource(url_item.openStream()), new StreamResult(out));
  
  /* Get multimedia image url */
  String[] thumbnail_urls;
  int num_multimedia = Integer.parseInt(doc_item.getElementsByTagName("num_multimedia").item(0).getFirstChild().getNodeValue());

  NodeList multimedia_thumbnails = doc_multimedia.getElementsByTagName("thumbnail");
  
  for(int i=0; i<num_multimedia; i++)
  {  
	  thumbnail_urls = new String[num_multimedia];
	  
	  thumbnail_urls[i] =  multimedia_thumbnails.item(i).getFirstChild().getFirstChild().getNodeValue();
    
	  out.print("<img src="+thumbnail_urls[i]+" />");
  }
  
%>

